package com.cskaoyan.app.service.impl;

import com.cskaoyan.app.bean.bo.AdminOrderListBO;
import com.cskaoyan.app.bean.bo.AdminOrderRefundBO;
import com.cskaoyan.app.bean.bo.AdminOrderReplyBO;
import com.cskaoyan.app.bean.bo.AdminOrderShipBO;
import com.cskaoyan.app.bean.common.BasePageInfo;
import com.cskaoyan.app.bean.common.CommonData;
import com.cskaoyan.app.bean.po.*;
import com.cskaoyan.app.bean.vo.AdminOrderChannelVO;
import com.cskaoyan.app.bean.vo.AdminOrderDetailVO;
import com.cskaoyan.app.enums.OrderStatus;
import com.cskaoyan.app.exception.ValidationException;
import com.cskaoyan.app.mapper.MarketCommentMapper;
import com.cskaoyan.app.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.app.mapper.MarketOrderMapper;
import com.cskaoyan.app.mapper.MarketUserMapper;
import com.cskaoyan.app.service.AdminOrderService;
import com.cskaoyan.app.util.StringsUtil;
import com.cskaoyan.app.util.ValidationUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author Shuuka
 * @email 1712430536@qq.com
 * @date 2022/12/28 23:37
 */
@Service
@Transactional
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Autowired
    MarketUserMapper marketUserMapper;

    @Autowired
    MarketCommentMapper marketCommentMapper;


    public static Short getCode(OrderStatus status){
        Short code = null;
        switch (status){
            case 未付款:
                code = 101;
                break;
            case 用户取消:
                code = 102;
                break;
            case 系统取消:
                code = 103;
                break;
            case 已付款:
                code = 201;
                break;
            case 申请退款:
                code = 202;
                break;
            case 已退款:
                code = 203;
                break;
            case 已发货:
                code = 301;
                break;
            case 用户收货:
                code = 401;
                break;
            case 系统收货:
                code = 402;
                break;
        }
        return code;
    }


    @Override
    public CommonData<MarketOrder> list(BasePageInfo basePageInfo, AdminOrderListBO adminOrderListBO)  {

        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketOrderExample example = new MarketOrderExample();
        example.setOrderByClause(basePageInfo.getSort()+" "+basePageInfo.getOrder());
        MarketOrderExample.Criteria criteria = example.createCriteria();
        if (adminOrderListBO.getUserId() != null) {
            criteria.andUserIdEqualTo(adminOrderListBO.getUserId());
        }
        if (!StringsUtil.isEmpty(adminOrderListBO.getOrderSn())) {
            criteria.andOrderSnEqualTo(adminOrderListBO.getOrderSn());
        }
        if (adminOrderListBO.getOrderStatusArray() != null) {
            criteria.andOrderStatusIn(adminOrderListBO.getOrderStatusArray());
        }
        if (adminOrderListBO.getStart() != null || adminOrderListBO.getEnd() != null) {
            criteria.andAddTimeBetween(adminOrderListBO.getStart(), adminOrderListBO.getEnd());
        }
        criteria.andDeletedEqualTo(false);
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(example);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);

        return CommonData.data(pageInfo);
    }

    @Override
    public List<AdminOrderChannelVO> channel() {
        List<AdminOrderChannelVO> adminOrderChannelVOS = marketOrderMapper.selectChannel();
        return adminOrderChannelVOS;
    }

    @Override
    public AdminOrderDetailVO detail(Integer id) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);

        String shipChannel = marketOrder.getShipChannel();
        String shipSn = marketOrder.getShipSn();
        marketOrder.setExpNo(shipSn);
        marketOrder.setExpCode(shipChannel);
        String expName = marketOrderMapper.ship(shipChannel);
        marketOrder.setExpName(expName);


        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria criteria = marketOrderGoodsExample.createCriteria();
        criteria.andOrderIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(marketOrderGoodsExample);
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(marketOrder.getUserId());
        AdminOrderDetailVO adminOrderDetailVO = new AdminOrderDetailVO(marketOrder, marketOrderGoods, marketUser);


        return adminOrderDetailVO;
    }

    @Override
    public Integer ship(AdminOrderShipBO adminOrderShipBO) {
        MarketOrder marketOrder = new MarketOrder();
        Date date = new Date();

        String shipChannel = adminOrderShipBO.getShipChannel();

        marketOrder.setId(adminOrderShipBO.getOrderId());
        marketOrder.setOrderStatus(getCode(OrderStatus.已发货));
        marketOrder.setShipSn(adminOrderShipBO.getShipSn());
        marketOrder.setShipChannel(shipChannel);
        marketOrder.setShipTime(date);
        marketOrder.setUpdateTime(date);

        marketOrder.setExpCode(shipChannel);
        String expName = marketOrderMapper.ship(shipChannel);
        marketOrder.setExpName(expName);
        marketOrder.setExpNo(adminOrderShipBO.getShipSn());

        Integer row = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return row;
    }

    @Override
    public Integer delete(Integer id) {
        Integer i = marketOrderMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public Integer refund(AdminOrderRefundBO adminOrderRefundBO) {
        MarketOrder marketOrder = new MarketOrder();
        Date date = new Date();
        marketOrder.setId(adminOrderRefundBO.getOrderId());
        marketOrder.setOrderStatus(getCode(OrderStatus.已退款));
        marketOrder.setRefundAmount(adminOrderRefundBO.getRefundMoney());
        marketOrder.setRefundTime(date);
        marketOrder.setRefundType("微信退款接口");
        marketOrder.setUpdateTime(date);
        marketOrder.setEndTime(date);
        Integer row = marketOrderMapper.updateByPrimaryKeySelective(marketOrder);
        return row;
    }

    @Override
    public Integer reply(AdminOrderReplyBO adminOrderReplyBO) {
        MarketComment marketComment = new MarketComment();
        marketComment.setId(adminOrderReplyBO.getCommentId());
        marketComment.setAdminContent(adminOrderReplyBO.getContent());
        marketComment.setUpdateTime(new Date());
        Integer row = marketCommentMapper.updateByPrimaryKeySelective(marketComment);
        return row;
    }
}
